﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class DatabaseHelper
    {
        private static string connStr = "server=.;database=Shcool;uid=sa;pwd=123456";
        private static string sql = "select * from Student";

        public static void PrintUserInfoRead()
        {
            //连接数据库
            string connStr = "server=.;database=Shcool;uid=sa;pwd=123456";
            //声明连接对象
            SqlConnection coon = new SqlConnection(connStr);
            coon.Open();
            //声明指令对象
            // string sql = "insert into Student values (3,'小明',17,'福建','女')";
            string sql = "select * from Student";
            SqlCommand cmd = new SqlCommand(sql, coon);
            //执行sql语句
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                int studentid = (int)sdr["studentid"];
                string studentname = (string)sdr["studentname"];
                int studentage = (int)sdr["studentage"];
                string studentadress = (string)sdr["studentaddress"];
                string studengsex = (string)sdr["studentsex"];
                Console.WriteLine(studentid + "\t" + studentname + "\t" + studentage + "\t" + studentadress + "\t" + studengsex);
            }
        }
        public static void PrintUserInfoAdapter()
        {
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, connStr);
            DataTable dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            for(int i = 0; i < dataTable.Rows.Count; i++)
            {
                int studentid = (int)dataTable.Rows[i]["studentid"];
                string studentname = (string)dataTable.Rows[i]["studentname"];
                int studentage = (int)dataTable.Rows[i]["studentage"];
                string studentadress = (string)dataTable.Rows[i]["studentaddress"];
                string studengsex = (string)dataTable.Rows[i]["studentsex"];
                Console.WriteLine(studentid + "\t" + studentname + "\t" + studentage + "\t" + studentadress + "\t" + studengsex);
            }
        }
        
}
}
